<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./vue.js"></script>
</head>
<body>
  <div id="app">
      <input  type="text" v-model="inputValue"><button v-on:click = BtnClick>提交</button>
      <ul>
         <todo-item v-bind:content = "item" 
                    v-bind:index = "index"
                    v-for="(item,index) in list"
                    @delete="try3">
        </todo-item>
        
      </ul>
  </div>


    <script>
    
    //点击增加的item会删除这个item，其它不变
    let TodoItem = {
        props:['content','index'],
        template:"<li @click='clickItemDelete'>{{content}}</li>",
        methods: {
            clickItemDelete:function(){
                //子组件使用emit让父组件听到自己的自定义事件
                this.$emit("delete",this.index)//这里的this是指props吗  //this.$emit触发当前实例上的事件
                console.log(1);
            }
        }
    }
    let app = new Vue({
        el: '#app',
        data:{
            list:[],
            inputValue:''
        },
        components: {
            TodoItem:TodoItem
        },
        methods: {
            BtnClick:function(){
                console.log(this.inputValue);
                this.list.push(this.inputValue);
                console.log(this.list);
                this.inputValue = '';
            },
            try3:function(index){
                this.list.splice(index,1)
                console.log(2);
            }
        }
    })
    </script>

</body>
</html> 